#!/usr/bin/env python
# -*- coding: utf-8 -*-


__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_36"
__date__    = "2014/01/05"
__license__ = 'LGPL'

# ------------------------------------------------------------
## インポート
# Python
import sys
sys.path.append(".")
sys.path.append("src")

import ConfigParser
import traceback
import codecs

# Mikan
from mikan.orm import new_mikan_orm

# ------------------------------------------------------------
## メイン処理
if __name__ == '__main__':
	config = ConfigParser.SafeConfigParser()
	try:
		config.readfp(codecs.open("etc/mikan.ini", "r", "utf8"))
	except:
		print(traceback.format_exc())

	ini = ConfigParser.SafeConfigParser()
	try:
		ini.readfp(codecs.open("./etc/orm.ini", "r", "utf8"))
	except:
		print(traceback.format_exc())

	try:
		t = config.get("orm", "type")
		orm = new_mikan_orm(None, t)
		orm.setup(config, "orm")
		orm.setup_orm(ini)
	except Exception, e:
		print(traceback.format_exc())

	newid = True
	update = True
	dohash = False
	for p in sys.argv:
		if p == "-i":
			newid = False
		elif p =="-n":
			update = False
		elif p =="-s":
			dohash = True

	f = open(sys.argv[len(sys.argv) - 1])
	d = f.read().decode("utf-8")
	f.close()
#	print d

	items = orm.imports("json", d, newid, update, dohash)
	for item in items:
		print item.values

	orm.commit()
